package com.cottagesystems.nbidl.session;

import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.openide.WizardDescriptor;

public final class StartSessionVisualPanel1 extends JPanel {
    
    /** Creates new form StartSessionVisualPanel1 */
    public StartSessionVisualPanel1() {
        initComponents();
        List profiles= SSHSessionSettings.getProfiles();
        if ( ! profiles.contains("default") ) profiles.add( "default" );        
        profileComboBox.setModel( new DefaultComboBoxModel( profiles.toArray( new String[profiles.size() ] ) ) );
        profileComboBox.setSelectedItem("default");
    }
    
    
    public String getName() {
        return "Step #1";
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        hostTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        translateFilenameCheckBox = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        hostFileSystemTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        localFileSystemTextField = new javax.swing.JTextField();
        shellPromptTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        idlPromptTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        startupCommands = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        unixStartupCommands = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        idlCommand = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        savePasswordCheckBox = new javax.swing.JCheckBox();
        profileComboBox = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        addProfileButton = new javax.swing.JButton();
        removeProfileButton = new javax.swing.JButton();

        hostTextField.setText("jbf@10.0.0.200");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "user@host:");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "Password:");

        org.openide.awt.Mnemonics.setLocalizedText(translateFilenameCheckBox, "Translate Filenames");
        translateFilenameCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        translateFilenameCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "host filesystem:");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, "local filesystem:");

        hostFileSystemTextField.setText("/net/spot/home/jbf/");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, "<html><i>(is known locally as)</i></html>");

        localFileSystemTextField.setText("J:/");

        shellPromptTextField.setText("jbf@spot >");
        shellPromptTextField.setMinimumSize(new java.awt.Dimension(60, 20));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, "Shell Prompt:");

        idlPromptTextField.setText("IDL>");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, "IDL Prompt:");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel8, "IDL Startup Commands:");

        startupCommands.setColumns(20);
        startupCommands.setRows(4);
        jScrollPane1.setViewportView(startupCommands);

        unixStartupCommands.setColumns(20);
        unixStartupCommands.setRows(5);
        jScrollPane2.setViewportView(unixStartupCommands);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel9, "Unix Startup Commands:");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel10, "IDL command:");

        org.openide.awt.Mnemonics.setLocalizedText(savePasswordCheckBox, "save password");
        savePasswordCheckBox.setToolTipText("<html>Your password is stored in a binary file in your Netbeans user directory.  A knowledgable person could gain access to your stored password.</html>");
        savePasswordCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        savePasswordCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        profileComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "default" }));
        profileComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profileComboBoxActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel11, "Profile:");

        org.openide.awt.Mnemonics.setLocalizedText(addProfileButton, "add...");
        addProfileButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        addProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addProfileButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(removeProfileButton, "remove");
        removeProfileButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        removeProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeProfileButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(passwordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(savePasswordCheckBox))
                            .add(hostTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(10, 10, 10)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(translateFilenameCheckBox)
                            .add(layout.createSequentialGroup()
                                .add(17, 17, 17)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel3)
                                    .add(jLabel4)
                                    .add(jLabel5))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(localFileSystemTextField)
                                    .add(hostFileSystemTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)))))
                    .add(layout.createSequentialGroup()
                        .add(jLabel6)
                        .add(19, 19, 19)
                        .add(shellPromptTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jLabel9)
                    .add(jLabel10)
                    .add(layout.createSequentialGroup()
                        .add(jLabel7)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(idlPromptTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jLabel8)
                    .add(layout.createSequentialGroup()
                        .add(10, 10, 10)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jScrollPane1)
                            .add(idlCommand)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 306, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(jLabel11)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(profileComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addProfileButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removeProfileButton)))
                .add(60, 60, 60))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel11)
                    .add(profileComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(addProfileButton)
                    .add(removeProfileButton))
                .add(14, 14, 14)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(hostTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(savePasswordCheckBox)
                    .add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(translateFilenameCheckBox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(hostFileSystemTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel5)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(localFileSystemTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(shellPromptTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel9)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel10)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(idlCommand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(idlPromptTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel8)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void profileComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profileComboBoxActionPerformed
        String profile1= (String)profileComboBox.getSelectedItem();
        if ( profile1!=null ) loadSettings();
    }//GEN-LAST:event_profileComboBoxActionPerformed
    
    private void removeProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeProfileButtonActionPerformed
        String profile1= (String)profileComboBox.getSelectedItem();
        if ( profile1.equals("default") ) return;
        SSHSessionSettings.removeProfile(profile1);
        DefaultComboBoxModel model= (javax.swing.DefaultComboBoxModel)profileComboBox.getModel();
        model.removeElement(profile1);
        loadSettings();
    }//GEN-LAST:event_removeProfileButtonActionPerformed
    
    private void addProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addProfileButtonActionPerformed
        //JTextField tf= new JTextField( "profile1" );
        String result= JOptionPane.showInputDialog( this, "Enter profile name" );
        System.err.println(result);
        String profile= result;
        DefaultComboBoxModel model= (javax.swing.DefaultComboBoxModel)profileComboBox.getModel();
        model.addElement( profile );
        SSHSessionSettings settings= SSHSessionSettings.retrieveSetting( profile );
        storeSettings(settings,profile);
        model.setSelectedItem( profile );
    }//GEN-LAST:event_addProfileButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addProfileButton;
    private javax.swing.JTextField hostFileSystemTextField;
    private javax.swing.JTextField hostTextField;
    private javax.swing.JTextField idlCommand;
    private javax.swing.JTextField idlPromptTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField localFileSystemTextField;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JComboBox profileComboBox;
    private javax.swing.JButton removeProfileButton;
    private javax.swing.JCheckBox savePasswordCheckBox;
    private javax.swing.JTextField shellPromptTextField;
    private javax.swing.JTextArea startupCommands;
    private javax.swing.JCheckBox translateFilenameCheckBox;
    private javax.swing.JTextArea unixStartupCommands;
    // End of variables declaration//GEN-END:variables
    
    
    void store( WizardDescriptor d ) {
        String profile= (String) profileComboBox.getSelectedItem();
        d.putProperty( "profile", profile );
        d.putProperty("password",new String( passwordField.getPassword() ));         
        SSHSessionSettings settings= SSHSessionSettings.retrieveSetting( (String) profileComboBox.getSelectedItem() );
        d.putProperty( "settings", settings );
        storeSettings( settings, profile );
    }
    
    /**
     * copies the UI fields into the persistent object
     */
    private void storeSettings( SSHSessionSettings settings, String profile ) {
        // save persistent settings for next session.        
        settings.setHost( hostTextField.getText() );
        boolean savePassword= savePasswordCheckBox.isSelected();
        if ( savePassword ) {
            settings.setPassword( new String( passwordField.getPassword() ) );
        } else {
            settings.setPassword( "" );
        }
        settings.setSavePassword( savePassword );
        settings.setIdlPrompt( idlPromptTextField.getText() );
        settings.setShellPrompt( shellPromptTextField.getText() );
        settings.setShellStartupCommands( unixStartupCommands.getText() );
        settings.setIdlStartupCommands( startupCommands.getText() );
        settings.setHostFileSystem( hostFileSystemTextField.getText() );
        settings.setLocalFileSystem( localFileSystemTextField.getText() );
        settings.setIdlCommand( idlCommand.getText() );
        settings.setTranslateFilename( translateFilenameCheckBox.isSelected() );
                
        SSHSessionSettings.storeSetting( profile, settings );
        
    }
    
    String getPassword() {
        return new String( passwordField.getPassword() );
    }
    
    private Object getProperty( WizardDescriptor d, String property, Object defaultt ) {
        Object r= d.getProperty(property);
        if ( r==null ) {
            return defaultt;
        } else {
            return r;
        }
    }
    void read( WizardDescriptor d ) {
        SSHSessionSettings settings= SSHSessionSettings.retrieveSetting("default");
        String profile= (String) d.getProperty( "profile" );
        if ( profile!=null ) profileComboBox.setSelectedItem(profile);
        passwordField.setText( (String) getProperty( d, "password", settings.getPassword()  ) );
        loadSettings();
    }

    private void loadSettings() {
        SSHSessionSettings settings= SSHSessionSettings.retrieveSetting((String)profileComboBox.getSelectedItem());
        hostTextField.setText( settings.getHost() );
        //passwordField.setText( settings.getPassword() );
        savePasswordCheckBox.setSelected( settings.isSavePassword() );
        shellPromptTextField.setText( settings.getShellPrompt()  );
        idlPromptTextField.setText( settings.getIdlPrompt() );
        translateFilenameCheckBox.setSelected( settings.isTranslateFilename() );
        hostFileSystemTextField.setText( settings.getHostFileSystem() );
        localFileSystemTextField.setText( settings.getLocalFileSystem() );
        unixStartupCommands.setText( settings.getShellStartupCommands()  );
        startupCommands.setText( settings.getIdlStartupCommands() );
        idlCommand.setText( settings.getIdlCommand() );
        
    }
}

